''' 与yolo无关，用于查看一些看不懂的函数的用法 '''

import matplotlib.pyplot as plt
import numpy as np
import cv2
from PIL import Image
import torch
import numpy as np






def list_slip():
    x= np.eye(5, 5)
    print(x)
    # print(x)
    print(x[:3, [1, 2]])
    # np.random.shuffle(x)
    print(x[:, 0:2])
    print(x[..., 2])
# list_slip()

def test_dataloader():
    input_shape = [416, 416]
    with open("./2007_train.txt",'r') as f:
        annotation_line = f.readlines()[0]
    # print(annotation_line)

    line = annotation_line.split()
    # 图片信息列表第一行为该图片的 绝对路径
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape

    # 这里开始是padding代码
    scale = min(w/iw, h/ih)
    nw = int(iw*scale)
    nh = int(ih*scale)
    dx = (w-nw)//2
    dy = (h-nh)//2

    # plt.imshow(image)
    # plt.xlabel('x: %s' % image.size[0], fontproperties='SimHei',fontsize=15)
    # plt.ylabel('y: %s' % image.size[1], fontproperties='SimHei',fontsize=15)
    # plt.show()
    image = image.resize((nw,nh), Image.BICUBIC)
    # plt.imshow(image)
    # plt.xlabel('x: %s' % image.size[0], fontproperties='SimHei',fontsize=15)
    # plt.ylabel('y: %s' % image.size[1], fontproperties='SimHei',fontsize=15)
    # plt.show()
    new_image = Image.new('RGB', (w,h), (128,128,128))
    # plt.imshow(new_image)
    # plt.xlabel('x: %s' % new_image.size[0], fontproperties='SimHei',fontsize=15)
    # plt.ylabel('y: %s' % new_image.size[1], fontproperties='SimHei',fontsize=15)
    # plt.show()
    new_image.paste(image, (dx, dy))
    plt.imshow(new_image)
    plt.xlabel('x: %s' % new_image.size[0], fontproperties='SimHei',fontsize=15)
    plt.ylabel('y: %s' % new_image.size[1], fontproperties='SimHei',fontsize=15)
    plt.show()
    image_data = np.array(new_image, np.float32)
    cv2.imshow("img", image_data)
    cv2.waitKey(0)


    # # padding后需要调整目标框坐标
    # box = np.array([np.array(list(map(int, box.split(',')))) for box in line[1:]])
    # box_data = np.zeros((len(box), 5))
    # if len(box) > 0:
    #     np.random.shuffle(box)
    #     box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
    #     box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
    #     box[:, 0:2][box[:, 0:2] < 0] = 0
    #     box[:, 2][box[:, 2] > w] = w
    #     box[:, 3][box[:, 3] > h] = h
    #     box_w = box[:, 2] - box[:, 0]
    #     box_h = box[:, 3] - box[:, 1]
    #     box = box[np.logical_and(box_w > 1, box_h > 1)]  # 保留有效框
    #     box_data = np.zeros((len(box), 5))
    #     box_data[:len(box)] = box

    # # 查看调整结果
    # classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
    # for label in box_data:
    #     print(label)